package com.video.main_video.manager.handle.uploadFile;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.upload.upload_module.manager.response.UploadResponse;
import com.video.main_video.api.Api;
import com.video.main_video.common.utils.ImgUtils;
import com.video.main_video.common.utils.VideoUtils;
import com.video.main_video.dao.entity.User;
import com.video.main_video.dao.entity.VideoInfo;
import com.video.main_video.dao.request.videoUpdate.VideoCoverGetRequest;
import com.video.main_video.dao.request.videoUpdate.VideoUploadRequest;

import com.video.main_video.manager.handle.ApiHandler;
import com.video.main_video.manager.mapper.VideoInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;

@Api
public class VideoCoverGetManager implements ApiHandler<VideoCoverGetRequest, UploadResponse> {

    @Autowired
    VideoInfoMapper videoInfoMapper;
    @Override
    public UploadResponse handler(VideoCoverGetRequest req) {
        UploadResponse res=new UploadResponse();
        String cover = getCoverHandler(req.getCutId());
        res.setCover(cover);
        res.setFileId(req.getCutId());
        return res;
    }

    private String getCoverHandler(String cutId){

        LambdaQueryWrapper<VideoInfo> qw = new LambdaQueryWrapper<>();
        qw.eq(VideoInfo::getCutId,cutId);
        VideoInfo info = videoInfoMapper.selectOne(qw);
        String path=info.getVideoDir()+"\\"+info.getVideoName();
        System.out.println(path);

        try {
            String coverPath = VideoUtils.extract(path, "./temp");
            String base64Image = ImgUtils.compressImageByFile(new File(coverPath));
            VideoUtils.deleteFile(coverPath);
            return "data:image/png;base64,"+base64Image;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
}
